package com.ruoyi.framework.login.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * ApiSessionFilter
 *
 * @author SERVER
 * @since 2024/3/29 14:15
 */
public interface ApiSessionKeyFilter {

    /**
     * 获取apiUrl 模块地址信息
     *
     * @return 模块名称
     */
    String getModelUrl();

    /**
     * url白名单前缀
     * @return
     */
    List<String> urlWhiteList();

    /**
     * 获取地址信息
     *
     * @return re
     */
    default String getUrlPrefix() {
        return "/api" + (StrUtil.isBlank(this.getModelUrl()) ? "" : "/" + this.getModelUrl());
    }

    /**
     * 过滤sessionKey信息过滤
     *
     * @param request  请求信息
     * @param response 返回信息
     * @return true/false
     */
    boolean filter(HttpServletRequest request, HttpServletResponse response) throws Exception ;



    /**
     * 向页面返回的
     *
     * @param response
     * @param r
     * @throws IOException
     */
    default void writerPrint(HttpServletResponse response, AjaxResult r) throws IOException {
        //返回超时错误码
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        PrintWriter writer = response.getWriter();
        writer.print(JSONUtil.parseObj(r));
        if (writer != null) {
            writer.close();
        }
    }
}
